var fs = require("fs");


fs.readFile('./city_data.json',function(err,data){
  if(err){
    console.log("bad")
  }else{
    var list = JSON.parse(data.toString());

    //生成省份文件
    // list.forEach(function (province) {
    //    delete province.children;
    // })
    // saveToFile(JSON.stringify(list),"./province.json")

    //生成城市文件
    list.forEach(function (province) {
      console.log("province:",province)
      if(province.children&&province.children.length>0){
        province.children.forEach(function (city) {
          delete city.children;
        })
      }

    })
    saveToFile(JSON.stringify(list),"./city.json")

  }
})

function  saveToFile(data,filename) {
  fs.writeFile(filename,data,{flag:'w',encoding:'utf-8',mode:'0666'},function(err){
    if(err){
      console.log("文件写入失败")
    }else{
      console.log("文件写入成功");

    }

  })
}


